package com.kuzma.itibo.controllers;

import java.util.List;

import com.kuzma.itibo.dao.AddressMapper;
import com.kuzma.itibo.dao.UserMapper;
import com.kuzma.itibo.dao.PostcardMapper;
import com.kuzma.itibo.datamodel.Address;
import com.kuzma.itibo.datamodel.Postcard;
import com.kuzma.itibo.datamodel.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {
	
	@Autowired
    private UserMapper userMapper;
	@Autowired
	private PostcardMapper postcardMapper;
	@Autowired
	private AddressMapper addressMapper;
 
    @RequestMapping("index.htm")
    public ModelAndView execute(Model model) {
    	User user = userMapper.getUserById(1L);
    	List<User> users = userMapper.getAllUsers();
        model.addAttribute("user", users.get(0));
        
        //user.setId(3L);
        //userMapper.savetUser(user);
        //userMapper.deleteUser(3L);
        //user.setName("New name");
        //userMapper.updateUser(user);
        
        model.addAttribute("postcard", postcardMapper.getPostcardById(1L));
        model.addAttribute("postcard", user.getPostcards().get(0));
        /*if (user.getPostcards() != null && !user.getPostcards().isEmpty()) {
        	Address address = user.getPostcards().get(0).getAddresses().get(0);
        	model.addAttribute("address", address);
        }*/
        model.addAttribute("address", addressMapper.getAddressById(1L));
        model.addAttribute("address", user.getPostcards().get(0).getAddresses().get(0));
        return new ModelAndView("start");
    }
}
